package training.osms.presentation.category;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import training.osms.business.controller.CategoryController;
import training.osms.business.model.Category;

@ManagedBean
@SessionScoped
public class EditCategory {

	private boolean categorySaved;
	private CategoryPanel panel;
	
	
	public String initialize(Category category){

		panel = new CategoryPanel(category.clone());
		categorySaved = false;
		return "editCategory";
	}
	
	public boolean isCategorySaved() {
		return categorySaved;
	}
	
	public void setCategorySaved(boolean categorySaved) {
		this.categorySaved = categorySaved;
	}
	
	public CategoryPanel getPanel() {
		return panel;
	}

	public void setPanel(CategoryPanel panel) {
		this.panel = panel;
	}

	public void editCategory(){
		CategoryController controller = new CategoryController();
		
		controller.updateCategory(panel.getCategory());
		
		this.categorySaved = true;
		
		FacesMessage message = new FacesMessage();
		message.setSummary("A categoria foi alterada com sucesso");
		message.setSeverity(FacesMessage.SEVERITY_INFO);
		FacesContext context = FacesContext.getCurrentInstance();
		context.addMessage(null, message);
	}
	
}
